/**
 * 邀请房间的管理器
 */
cc.Class({
    extends: cc.Component,

    properties: {
        mainManager: {
            default: null,
            visible: false,
        },
        /**
         * 开始按钮
         */
        startBtn: cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._player = new Array("","");
    },

    start () {
        
    },

    update (dt) {
        
    },
    
    /**
     * 初始化
     */
    init () {
        //显示房间列表
        console.log(this._player);
        GameData.IsInRoom = true;
        //默认不可以开始游戏
        this.startBtn.interactable = false;
    },

    /**
     * 点击邀请好友按钮
     */
    onInviteClick () {
        if (!CC_WECHATGAME) return;
        wx.shareAppMessage({
            title: "房已开好，等你来战！",
            query: "Share=Battle&" + "RoomNo=" + GameData.RoomNo,
        });
    },

    /**
     * 点击返回主界面按钮
     */
    onBackClick () {
        this.mainManager.changePanel('MenuPanel');
        this.mainManager.closeOpenDataCanvas();
        //通知另一个房间的玩家
        var self = this;
        GameData.IsInRoom = false;
    },

    /**
     * 点击开始游戏按钮
     */
    onStartClick () {
        //只有房间内两个玩家都存在时才可以开始
        if (this._player[0] == "" || this._player[1] == "") return;
        console.log('开始游戏');
        //往服务器发送数据
        //两个玩家同时收到游戏开始的消息时统一进入游戏
        this.mainManager.sendSocketMessage({StartGame: {
            RoomNo: GameData.RoomNo,
        }});
        //显示加载提示
        if (CC_WECHATGAME) {
            wx.showLoading({
                title: '等待开局',
            })
        }
    },

    /**
     * 接收房间号
     * @param {string} roomNo 房间号
     */
    recieveRoomNo (roomNo) {
        GameData.RoomNo = roomNo;
    },

    /**
     * 设置房间玩家信息
     * @param {number} index 下标
     * @param {string} openID ID
     */
    setPlayer (index, openID) {
        if (this._player == undefined) {
            this._player = new Array("","");
        }
        this._player[index] = openID;
        console.log("玩家" + index + ":ID" + openID);
        //更新显示
        this.showPlayerInfo();
        //设置对手的信息
        if (index != this.getSelfPos()) {
            GameData.OpponentID = openID;
        }
    },

    /**
     * 设置房间玩家信息
     * @param {json} roomInfo 房间两人信息
     */
    setRoomInfo (roomInfo) {
        if (this._player == undefined) {
            this._player = new Array("","");
        }
        this._player[0] = roomInfo.Left;
        this._player[1] = roomInfo.Right;
        //更新显示
        this.showPlayerInfo();
        //判断是否有两个玩家可以开始游戏
        this.startBtn.interactable = this._player[0] != "" && this._player[1] != "";
    },

    /**
     * 显示房间内玩家信息
     */
    showPlayerInfo () {
        this.mainManager.postToOpenData({
            message: 'ShowInviteRoom',
            left: this._player[0],
            right: this._player[1]
        })
    },

    /**
     * 获取自身在房间中的位置
     * @returns 位置
     */
    getSelfPos () {
        for (var i = 0; i < this._player.length; i++) {
            if (this._player[i] == GameData.OpenID){
                return i;
            }
        }
        return -1;
    },

});
